import React from 'react';
const R = require('ramda');
import { Form, Button, Modal,  Tabs, message } from 'antd';

import EditCustomer from '../components/EditCustomer';
import AgreementInfo from '../components/AgreementInfo';
import CusCredit from '../components/CusCredit';
import Linkman from '../components/Linkman';
import RecAcc from '../components/RecAcc';
import Product from '../components/Product';
import Order from '../components/Order';
import BusinessAgreement from '../components/BusinessAgreement';
import StatementList from './StatementList';
import OrderBilldetailList from '../components/OrderBilldetailList';
import moment from 'moment';
import ReconciliationList from '../components/ReconciliationList';
import ExtraInfo from '../components/ExtraInfo';
import RightBox from "../../commons/RightBox";
import RIGHT from "../../../utils/RightConstants";
import Constants from "../../../utils/Constants";

moment.locale('zh-cn');

const TabPane = Tabs.TabPane;
const FormItem = Form.Item;

export default class CustomerDetail extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            searchConditions: {},
            open: false,
            disabled: true,
            tabActiveKey: "1",
            cstid: "",
            editCustomerDisable: false,
            showOther: false,
            extraUpdating: false,    //供应商交接单是否再次编辑中,
            personAllocation: false    //人员分配
        };
    }


    handleOpen = (disabled, searchConditions, tabActiveKey, cstid, editCustomerDisablel, type,type1) => {
        type = R.isNil(type) ? "customerManage" : type;

        let showOther = false;

        if ("customerDetail" == type) {
            showOther = true;
            this.setState({ personAllocation: true })
        }

        this.setState({
            open: true, disabled: disabled, searchConditions: searchConditions,
            tabActiveKey: tabActiveKey, cstid: cstid, editCustomerDisable: editCustomerDisablel,type1:type1,
            showOther: showOther
        });
    }


    handleResetFields = () => {
        this.state.cstid = "";
        if (this.editCustomer) this.editCustomer.handleResetFields();
        if (this.linkman) this.linkman.handleResetFields();
        if (this.agreementInfo) this.agreementInfo.handleResetFields();
        if (this.recAcc) this.recAcc.handleResetFields();
        if (this.product) this.product.handleResetFields();
        if (this.drawer) this.drawer.handleResetFields();
        if (this.order) this.order.handleResetFields();
    }

    handleSaveAndSubmit = (status) => {
        if (this.state.tabActiveKey === "1") {
            if (this.editCustomer) {
                this.editCustomer.checkCustomerInfo(this.props.dispatch, this.onSuccess, this.state.cstid, status)

            }

        }
    }

    onSuccess = () => {
        this.props.handleSearch();
        this.handleResetFields();
        setTimeout(this.handleCancel, 1000);
    }


    handleSaveAttachment = () => {
        if (this.state.tabActiveKey === "3" || this.state.tabActiveKey === "11") {
            if (this.agreementInfo) {
                this.agreementInfo.handleSaveAttachment();
            }
        }
    }

    credit = () => {
        debugger;
        if (this.state.tabActiveKey === "3" || this.state.tabActiveKey === "11") {
            if (this.cusCredit) {
                this.cusCredit.handleSaveAttachment();
            }
        }
    }

    handleCancel = (flag) => {
        if (flag) {
            this.handleResetFields();
        }
        this.setState({ tabActiveKey: "1", open: false, searchConditions: {} });
        if (!R.isNil(this.businessAgreement) && !R.isEmpty(this.businessAgreement)) {
            this.businessAgreement.handleResetFields();
        }

    }

    handleChangeTabe = (key) => {
        this.setState({ tabActiveKey: key })
    }

    handleSaveExtra = () => {
        this.extraInfo.handleConfirm();
    }

    handleReUpdateExtra = (extraUpdating) => {
        this.setState({ extraUpdating })
    }

    render() {

        let footer = [];

        let dataSource = R.isNil(this.props.dataSource) ? {} : this.props.dataSource;
        let approvalTit = dataSource && dataSource.businessType === "2" ? "保存并提交" : "保存并提交审核";

        if ((this.state.tabActiveKey === "1") && !this.state.editCustomerDisable) {
            if (dataSource.csttype === "") {
                footer.push(<Button key="submit" size="large" loading={this.state.submitLoading}
                    disabled={this.state.disabled} onClick={this.handleSaveAndSubmit.bind(this, null)}
                    style={{ display: this.state.tabActiveKey === "1" || this.state.tabActiveKey === "3" || this.state.disabled ? "inline-block" : "none" }}>保存</Button>);
                footer.push(<Button key="primary" size="large" loading={this.state.submitLoading}
                    disabled={this.state.disabled} onClick={this.handleSaveAndSubmit.bind(this, 2)}
                    style={{ display: this.state.tabActiveKey === "1" || this.state.disabled ? "inline-block" : "none" }}>{approvalTit}</Button>);
            } else {
                footer.push(<Button key="submit" size="large" loading={this.state.submitLoading}
                    disabled={this.state.disabled} onClick={this.handleSaveAndSubmit.bind(this, null)}
                    style={{ display: this.state.tabActiveKey === "1" || this.state.tabActiveKey === "3" || this.state.disabled ? "inline-block" : "none" }}>保存</Button>);
                footer.push(<Button key="primary" size="large" loading={this.state.submitLoading}
                    disabled={this.state.disabled} onClick={this.handleSaveAndSubmit.bind(this, 2)}
                    style={{ display: this.state.tabActiveKey === "1" || this.state.disabled ? "inline-block" : "none" }}>提交</Button>);
            }
        }

        if ((this.state.tabActiveKey === "3" && this.state.editCustomerDisable != true)) {
            let rights = R.isNil(sessionStorage.getItem(Constants.USERRIGHTS)) ? [] : sessionStorage.getItem(Constants.USERRIGHTS);
            if (R.contains(RIGHT.SUPPLIER.SUPPLIER_MANAGE_ATTACH_SAVE)(rights)) {
                footer.push(<Button key="submit" size="large" loading={this.state.submitLoading} disabled={this.state.disabled} onClick={this.handleSaveAttachment.bind(this)}
                    style={{ display: this.state.tabActiveKey === "1" || this.state.tabActiveKey === "3" || this.state.tabActiveKey === "11"|| this.state.disabled ? "inline-block" : "none" }}>保存附件</Button>);
            }
        }
        if ((this.state.tabActiveKey === "11" && this.state.editCustomerDisable != true)) {
            let rights = R.isNil(sessionStorage.getItem(Constants.USERRIGHTS)) ? [] : sessionStorage.getItem(Constants.USERRIGHTS);
            if (R.contains(RIGHT.SUPPLIER.SUPPLIER_MANAGE_ATTACH_SAVE)(rights)) {
                footer.push(<Button key="submit" size="large" loading={this.state.submitLoading} disabled={this.state.disabled} onClick={this.credit.bind(this)}
                                    style={{ display: this.state.tabActiveKey === "1" || this.state.tabActiveKey === "3" || this.state.tabActiveKey === "11"|| this.state.disabled ? "inline-block" : "none" }}>保存附件</Button>);
            }
        }

        if (this.state.tabActiveKey === "10") {
            if (this.props.extra && !this.state.extraUpdating) {
                let rights = R.isNil(sessionStorage.getItem(Constants.USERRIGHTS)) ? [] : sessionStorage.getItem(Constants.USERRIGHTS);
                if (R.contains(RIGHT.CUSTOMER.CUSTOMER_MANAGE_EXTRA_UPDATE)(rights)) {
                    footer.push(<Button key="update" size="large" onClick={this.handleReUpdateExtra.bind(this, true)}>重新编辑</Button>);
                }
            } else {
                let rights = R.isNil(sessionStorage.getItem(Constants.USERRIGHTS)) ? [] : sessionStorage.getItem(Constants.USERRIGHTS);
                if (R.contains(RIGHT.CUSTOMER.CUSTOMER_MANAGE_EXTRA_SAVE)(rights)) {
                    footer.push(<Button key="submit" size="large" loading={this.state.submitLoading} onClick={this.handleSaveExtra.bind(this)}>保存</Button>);
                }
            }
        }

        footer.push(<Button key="cancel" size="large" disabled={this.state.disabled} onClick={this.handleCancel.bind(this, true)}>取消</Button>);


        let widths = { "1": 900, "2": 900, "3": 1080, "4": 900, "5": 900, "6": 900, "7": 900, "8": 900, "9": 1200, "10": 1200, "11": 1200 };

        let reason = (R.isEmpty(this.props.approvaList) || R.isNil(this.props.approvaList)) ? "" : "__审核备注:" + this.props.approvaList[0].memo;


        let fkManager = R.isNil(this.editCustomer) ? null : R.isNil(this.editCustomer.props.dataSource) ? null : this.editCustomer.props.dataSource.fkmanager;
        return (
            <Modal maskClosable={false} footer={this.state.disabled ? null : footer} visible={this.state.open} title={this.state.showOther == true ? "供应商详情" + reason : "编辑供应商" + reason} width={widths[this.state.tabActiveKey]} wrapClassName="vertical-center-modal" onCancel={this.handleCancel}>
                <Tabs activeKey={this.state.tabActiveKey} tabPosition={'left'} onChange={this.handleChangeTabe.bind(this)} >
                    <TabPane tab="基本信息" key="1">{<EditCustomer
                        wrappedComponentRef={(inst) => this.editCustomer = inst}
                        personAllocation={this.state.personAllocation}
                        disabled={this.state.editCustomerDisable} type={"edit"}
                        manager={this.props.manager}
                        dataSource={this.props.dataSource}
                        blacklist={this.props.blacklist}
                        dispatch={this.props.dispatch}
                        factoryUser={this.props.factoryUser} businessType={dataSource && dataSource.businessType ? dataSource.businessType : ""}
                    />}</TabPane>


                    <TabPane tab="采购合同管理" key="2">{<BusinessAgreement
                        wrappedComponentRef={(inst) => this.businessAgreement = inst}
                        disabled={false}
                        businessAgreement={this.props.businessAgreement}
                        cstid={this.state.cstid} dispatch={this.props.dispatch}
                        dataSource={this.props.dataSource}
                        fkManager={fkManager}
                        type1={this.state.type1}

                    />}</TabPane>


                    <TabPane tab="协议与资质" key="3">{<AgreementInfo
                            wrappedComponentRef={(inst) => this.agreementInfo = inst}
                            disabled={this.state.editCustomerDisable}
                            dispatch={this.props.dispatch}
                            cstid={this.state.cstid}
                            dataSource={this.props.attachments}
                            cstInfo={this.props.dataSource}
                            customerHandleCancel={this.handleCancel}/>
                    }
                    </TabPane>


                    <TabPane tab="联系人信息" key="4">{<Linkman wrappedComponentRef={(inst) => this.linkman = inst}
                        disabled={this.state.disabled}
                        linkmanPage={this.props.linkmanPage}
                        linkmanList={this.props.linkmanList}
                        cstid={this.state.cstid} dispatch={this.props.dispatch}
                        linkman={this.props.linkman} />}</TabPane>


                    {this.state.showOther == true ?
                        <TabPane tab="收款人账户" key="5">{<RecAcc wrappedComponentRef={(inst) => this.recAcc = inst}
                            disabled={this.state.disabled}
                            recAccPage={this.props.recAccPage}
                            recAccList={this.props.recAccList}
                            cstid={this.state.cstid} dispatch={this.props.dispatch}
                            recAcc={this.props.recAcc} />}</TabPane>
                        : ""}


                    {this.state.showOther == true ?
                        <TabPane tab="产品管理" key="6">{<Product wrappedComponentRef={(inst) => this.product = inst}
                            disabled={this.state.disabled}
                            productPage={this.props.productPage}
                            productList={this.props.productList}
                            cstid={this.state.cstid} dispatch={this.props.dispatch}
                            product={this.props.product}
                            businessType={dataSource && dataSource.businessType ? dataSource.businessType : ""}/>}</TabPane>
                        : ""}

                    {this.state.showOther == true ?
                        <TabPane tab="订单管理" key="7">{<Order wrappedComponentRef={(inst) => this.order = inst}
                             disabled={this.state.disabled}
                             orderManage={this.props.orderManage}
                             customerManage={this.props.customerManage}
                             customs={this.props.customs}
                             supplierId={this.state.cstid}
                             dispatch={this.props.dispatch}/>}
                        </TabPane>
                        : ""}



                    {this.state.showOther == true ?
                        <TabPane tab="结算单" key="8">{<StatementList wrappedComponentRef={(inst) => this.statementList = inst}
                            disabled={this.state.disabled}
                            settlement={this.props.settlement}
                            capital={this.props.capital}
                            cstid={this.state.cstid} dispatch={this.props.dispatch}
                        />}</TabPane>
                        : ""}


                    {/*{this.state.showOther == true ?*/}
                        {/*<TabPane tab="供应商对账单" key="9">{<ReconciliationList wrappedComponentRef={(inst) => this.reconciliationList = inst}*/}
                        {/*disabled={this.state.disabled}*/}
                        {/*dataSource={this.props.dataSource}*/}
                        {/*customerReport={this.props.customerReport}*/}
                        {/*cstid={this.state.cstid} dispatch={this.props.dispatch}*/}
                        {/*/>}</TabPane>*/}
                        {/*: ""}*/}

                    {/*{this.state.showOther == true ?*/}
                        {/*<TabPane tab="供应商交接单" key="10">{*/}
                        {/*<ExtraInfo wrappedComponentRef={(inst) => this.extraInfo = inst}*/}
                        {/*disabled={this.state.disabled} extraUpdating={this.state.extraUpdating}*/}
                        {/*cstid={this.state.cstid} dispatch={this.props.dispatch} handleReUpdateExtra={this.handleReUpdateExtra}*/}
                        {/*dataSource={this.props.extra} />}</TabPane>*/}
                        {/*: ""}*/}


                        <TabPane tab="制作信用证" key="11">
                            {<CusCredit
                                wrappedComponentRef={(inst) => this.cusCredit = inst}
                                disabled={this.state.editCustomerDisable}
                                dispatch={this.props.dispatch}
                                cstid={this.state.cstid}
                                dataSource={this.props.attachments}
                                cstInfo={this.props.dataSource}
                                customerHandleCancel={this.handleCancel}/>
                            }
                        </TabPane>


                </Tabs>
            </Modal>
        );
    }
}